package org.jeecg.modules.lab.chemicalDiscard.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.lab.chemical.entity.LabChemical;
import org.jeecg.modules.lab.chemical.service.ILabChemicalService;
import org.jeecg.modules.lab.chemicalDiscard.entity.LabChemicalDiscard;
import org.jeecg.modules.lab.chemicalDiscard.entity.LabChemicalDiscardDescribe;
import org.jeecg.modules.lab.chemicalDiscard.entity.LabChemicalDiscardTo;
import org.jeecg.modules.lab.chemicalDiscard.service.ILabChemicalDiscardDescribeService;
import org.jeecg.modules.lab.chemicalDiscard.service.ILabChemicalDiscardService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 标物的报废
 * @Author: jeecg-boot
 * @Date: 2023-08-18
 * @Version: V1.0
 */
@Api(tags = "标物的报废")
@RestController
@RequestMapping("/chemicalDiscard/labChemicalDiscard")
@Slf4j
public class LabChemicalDiscardController extends JeecgController<LabChemicalDiscard, ILabChemicalDiscardService> {
    @Autowired
    private ILabChemicalDiscardService labChemicalDiscardService;
    @Autowired
    private ILabChemicalDiscardDescribeService labChemicalDiscardDescribeService;
    @Autowired
    private ILabChemicalService labChemicalService;

    @Autowired
    private ILabCronJobService labCronJobService;

    /**
     * 分页列表查询
     *
     * @param labChemicalDiscard
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "标物的报废-分页列表查询")
    @ApiOperation(value = "标物的报废-分页列表查询", notes = "标物的报废-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<LabChemicalDiscard>> queryPageList(LabChemicalDiscard labChemicalDiscard,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                           HttpServletRequest req) {
        QueryWrapper<LabChemicalDiscard> queryWrapper = QueryGenerator.initQueryWrapper(labChemicalDiscard, req.getParameterMap());
        Page<LabChemicalDiscard> page = new Page<LabChemicalDiscard>(pageNo, pageSize);
        IPage<LabChemicalDiscard> pageList = labChemicalDiscardService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param labChemicalDiscardTo
     * @return
     */
    @AutoLog(value = "标物的报废-添加")
    @ApiOperation(value = "标物的报废-添加", notes = "标物的报废-添加")
//	@RequiresPermissions("chemicalDiscard:lab_chemical_discard:add")
    @PostMapping(value = "/add")
    @Transactional
    public Result<String> add(@RequestBody LabChemicalDiscardTo labChemicalDiscardTo) {
        LabChemicalDiscard labChemicalDiscard = new LabChemicalDiscard();
        BeanUtils.copyProperties(labChemicalDiscardTo, labChemicalDiscard);
        labChemicalDiscard.setId(UUID.randomUUID().toString());
        List<LabChemical> list = new ArrayList<>();
        //添加进报废详情表
        labChemicalDiscardTo.getListDiscard().stream().forEach(e -> {
            e.setDiscardId(labChemicalDiscard.getId());
            e.setId(UUID.randomUUID().toString());//设置清单中数据的id
            LabChemical labChemical = new LabChemical();
            labChemical.setId(e.getChemicalId());
            labChemical.setStatus("报废");
            labChemical.setInventoryNow(Double.valueOf(e.getInventoryNow()) - Double.valueOf(e.getDiscardNumber()));
            labChemical.setSynFlag("1");
            list.add(labChemical);
        });
        labChemicalDiscardService.save(labChemicalDiscard);
        labChemicalDiscardDescribeService.saveBatch(labChemicalDiscardTo.getListDiscard());
        //修改基本表中标准物质的状态
        labChemicalService.updateBatchById(list);

        if (MyEnum.sync) {
            long time = new Date().getTime();
            ArrayList<LabCronJob> jobs = new ArrayList<>();
            jobs.add(new LabCronJob("lab_chemical_discard", "0", time, 1));
            jobs.add(new LabCronJob("lab_chemical_discard_describe", "0", time, 2));
            jobs.add(new LabCronJob("lab_chemical", "1", time, 3));
            labCronJobService.saveBatch(jobs);
        }

        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param labChemicalDiscard
     * @return
     */
    @AutoLog(value = "标物的报废-编辑")
    @ApiOperation(value = "标物的报废-编辑", notes = "标物的报废-编辑")
//	@RequiresPermissions("chemicalDiscard:lab_chemical_discard:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody LabChemicalDiscard labChemicalDiscard) {
        labChemicalDiscardService.updateById(labChemicalDiscard);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "标物的报废-通过id删除")
    @ApiOperation(value = "标物的报废-通过id删除", notes = "标物的报废-通过id删除")
//	@RequiresPermissions("chemicalDiscard:lab_chemical_discard:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        LambdaQueryWrapper<LabChemicalDiscardDescribe> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LabChemicalDiscardDescribe::getDiscardId, id);
        List<LabChemicalDiscardDescribe> list = labChemicalDiscardDescribeService.list(wrapper);
        if (list.isEmpty()) {
            labChemicalDiscardService.removeById(id);
            return Result.OK("删除成功!");
        } else {
            return Result.error("确认删除清单信息");
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "标物的报废-批量删除")
    @ApiOperation(value = "标物的报废-批量删除", notes = "标物的报废-批量删除")
//	@RequiresPermissions("chemicalDiscard:lab_chemical_discard:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.labChemicalDiscardService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "标物的报废-通过id查询")
    @ApiOperation(value = "标物的报废-通过id查询", notes = "标物的报废-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LabChemicalDiscard> queryById(@RequestParam(name = "id", required = true) String id) {
        LabChemicalDiscard labChemicalDiscard = labChemicalDiscardService.getById(id);
        if (labChemicalDiscard == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(labChemicalDiscard);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param labChemicalDiscard
     */
//    @RequiresPermissions("chemicalDiscard:lab_chemical_discard:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabChemicalDiscard labChemicalDiscard) {
        return super.exportXls(request, labChemicalDiscard, LabChemicalDiscard.class, "标物的报废");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
//    @RequiresPermissions("chemicalDiscard:lab_chemical_discard:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabChemicalDiscard.class);
    }

}
